<?php

/**
 * 权限管理
 *
*/
namespace app\admin\controller;

use app\admin\model\Permission as PowerModel;
use think\Request;

class Permission extends Base
{
    protected $model;
    protected $request;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new PowerModel();

        $this->request = $request;
    }

    public function index()
    {
        $data = $this->model->select();

        $data = obj_tree($data);
        
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $name = isset($param['name']) ? trim($param['name']) : '';

            if ($name == '') {
                $this->error('名称不能为空');
            }

            $pid = isset($param['pid']) ? intval($param['pid']) : 0;
            $method = isset($param['method']) ? trim($param['method']) : '';
            $menu = isset($param['menu']) ? intval($param['menu']) : 0;
            $note = isset($param['note']) ? trim($param['note']) : '';

            try {
                $this->model->save([
                    'pid' => $pid,
                    'name' => $name,
                    'method' => $method,
                    'menu' => $menu,
                    'note' => $note,
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:" . $msg);
            }
            $this->success('添加成功', '/permission/index');
        } else {
            $powers = $this->model->where('status', 1)->field('id, pid, name')->select();

            $powers = obj_tree($powers);

            $this->assign('powers', $powers);
            return $this->fetch();
        }
    }

    public function edit($id = null)
    {
        $data = $this->model->find($id);

        if ($this->request->isPost()) {
            $param = $this->request->param();
            
            $name = isset($param['name']) ? trim($param['name']) : '';

            if ($name == '') {
                $this->error('名称不能为空');
            }

            $pid = isset($param['pid']) ? intval($param['pid']) : 0;
            $method = isset($param['method']) ? trim($param['method']) : '';
            $menu = isset($param['menu']) ? intval($param['menu']) : 0;
            $status = isset($param['status']) ? intval($param['status']) : 1;
            $note = isset($param['note']) ? trim($param['note']) : '';

            try {
                $this->model->save([
                    'pid' => $pid,
                    'name' => $name,
                    'method' => $method,
                    'menu' => $menu,
                    'status' => $status,
                    'note' => $note,
                ], ['id' => $id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:" . $msg);
            }
            $this->success('修改成功', '/permission/index');
        } else {
            $powers = $this->model->where('status=1')->field('id, pid, name')->select();

            $powers = obj_tree($powers);

            $this->assign('powers', $powers);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function del()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $id = $param['id'];

            if ($this->model->where('pid', 'IN', $id)->value('name')) {
                $this->error('子权限不为空, 不可删除');
            }
            
            if ($this->model->destroy($id)) {
                return ['code' => 2, 'msg' => '操作成功'];
            } else {
                $this->error('删除失败, 请稍后重试');
            }
        }
    }
}
